package com.hopes.bizz.poll.common.loigc;

import com.hopes.bizz.poll.common.constant.AlarmConstant;
import com.hopes.bizz.poll.common.constant.DateConstant;

import java.util.Date;


public class AlarmLogic {

    /**
     * 构建持续时间描述信息
     *
     * @param alarmTime
     * @param removeTime
     * @param durationTime
     * @param alarmStatus
     * @return
     */
    public static String buildDurationTimeDesc(Date alarmTime, Date removeTime, Long durationTime, Integer alarmStatus) {
        if (alarmTime == null) {
            return null;
        }

        StringBuilder res = new StringBuilder();
        Long durationSecond = durationTime;
        if (durationSecond == 0L) {
            res.append("从未上线");
            return res.toString();
        }
        if (durationSecond == null) {
            if (removeTime == null && AlarmConstant.ALARM_STATUS_NOT == alarmStatus) {
                removeTime = new Date();
            }
            if (removeTime != null) {
                durationSecond = (removeTime.getTime() - alarmTime.getTime()) / 1000;
            }
        }

        if (durationSecond == null) {
            return null;
        }

        if (durationSecond < 60) {
            res.append("小于一分钟");
        } else {
            long time = durationSecond / DateConstant.ONE_DAY;
            if (time > 0) {
                res.append(time).append("天");
                durationSecond = durationSecond % DateConstant.ONE_DAY;
            }
            time = durationSecond / DateConstant.ONE_HOUR;
            if (time > 0) {
                res.append(time).append("小时");
                durationSecond = durationSecond % DateConstant.ONE_HOUR;
            }
            time = durationSecond / DateConstant.ONE_MINUTE;
            if (time > 0) {
                res.append(time).append("分钟");
            }
        }
        return res.toString();
    }

    /**
     * 构建持续时间描述信息
     *
     * @param durationSecond
     * @return
     */
    public static String buildDurationTimeDesc(Long durationSecond) {
        StringBuilder res = new StringBuilder();
        if (durationSecond == 0L) {
            res.append("从未上线");
            return res.toString();
        }

        if (durationSecond < 60) {
            res.append("小于一分钟");
        } else {
            long time = durationSecond / DateConstant.ONE_DAY;
            if (time > 0) {
                res.append(time).append("天");
                durationSecond = durationSecond % DateConstant.ONE_DAY;
            }
            time = durationSecond / DateConstant.ONE_HOUR;
            if (time > 0) {
                res.append(time).append("小时");
                durationSecond = durationSecond % DateConstant.ONE_HOUR;
            }
            time = durationSecond / DateConstant.ONE_MINUTE;
            if (time > 0) {
                res.append(time).append("分钟");
            }
        }
        return res.toString();
    }
}
